package org.kotboot.service;
import org.kotboot.dao.CommodityDao;
import org.kotboot.dao.CommodityDetailDao;
import org.kotboot.domain.Commodity;
import org.kotboot.domain.CommodityDetail;
import org.kotboot.domainext.ExtCommodity;
import org.kotboot.enums.ECommodityState;
import org.kotboot.kit.K;
import org.kotboot.kit.KMap;
import org.kotboot.module.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * jisong.sun 2017/9/19 18:06
 */
@Service
public class CommodityServ {

    private static Logger log = LoggerFactory.getLogger(CommodityServ.class);

    @Resource
    private CommodityDao commodityDao;

    @Resource
    private CommodityDetailDao commodityDetailDao;

    /**
     * 分页 查询 列表
     * @param params
     * @return
     */
    public R page(KMap params) {
        Commodity select = select();
        params.orderBy("ctime desc");
        params.startPage();
        List<Commodity> listRes = commodityDao.select(select);
        PageInfo<Commodity> res = new PageInfo<Commodity>(listRes);
        return R.OK(res);
    }

    /**
     * ID 查询 
     * @param commodityid
     * @return
     */
    public R quaryById(String commodityid) {
        Commodity select = select();
        Long id_L = NumberUtils.toLong(commodityid);
        select.setId(NumberUtils.toLong(commodityid));
        Commodity commodity = commodityDao.selectOne(select);
        log.info("查询结果:{}",commodity);
        CommodityDetail commodityDetail = commodityDetailDao.byCommodityid(id_L);
        log.info("查询明细结果:{}",commodityDetail);
        ExtCommodity ec = K.transBean(commodity,ExtCommodity.class);
        ec.setDetails(commodityDetail.getDetails());
        return R.OK(ec);
    }


    private Commodity select(){
        Commodity select = new Commodity();
        select.setState(ECommodityState.PASSED);
        return select;
    }
}
